<?php

namespace App\Http\Controllers;

use App\Http\Requests\CourseCreate;
use App\Models\Course;
use App\Services\CourseService;
use Illuminate\Http\Request;

class CourseController extends Controller
{
    public function list(Request $request){

        $res = CourseService::Courselist($request);

        return resSucc(formatPage($res), "课表");
    }

    public function check(CourseCreate $request){

        $list = $request->validated();

        foreach($list['list'] as &$course)
        {
            $course = CourseService::checkCourseRepeat($course);
        }

        return resSucc($list['list'], "排课");
    }

    public function create(CourseCreate $request){

        $list = $request->validated();

        foreach($list['list'] as &$item)
        {
            $item['course_type'] = $item['type'];
            $item['school_id'] = $request->school_id;
            $item['campus_id'] = $request->campus_id;
            $item['end_time'] = $item['start_time'] + $item['len'] * 60;

            unset($item['type']);
            unset($item['conflict']);
        }

        Course::insert($list['list']);

        return resSucc(count($list['list']), "排课成功");
    }
}
